﻿using System;

namespace EmbeddedSharp.Core.Function.Arithmetics {

    /// <summary>
    /// a = 1 + 3;
    /// echo(a);
    /// # result is 4
    /// </summary>
    [Function("plus", "Plus two numbers", "a=3+3;", "a==6")]
    public class AddNode : OperatorNode {

        /// <summary>
        /// Default constructor
        /// </summary>
        public AddNode()
            : base("+", 1) {
        }

        public override object Accept(IVisitor visitor) {
            return visitor.Visit(this);
        }

        public override FuncNode GetReversedFunction(int index) {
            return new SubNode();
        }

        public override object Evaluate() {
            object l = Left.Evaluate();
            object r = Right.Evaluate();

            if (Utils.IsNumeric(l) && Utils.IsNumeric(r)) {
                double lr = Convert.ToDouble(l);
                double rr = Convert.ToDouble(r);

                return (lr + rr).ToString();
            } else if (l is string || r is string) {
                string lr = Convert.ToString(l);
                string rr = Convert.ToString(r);

                return lr + rr;
            } else if (l is ListNode || r is ListNode) {
                if (l is ListNode && r is ListNode) {
                } else {
                    if (l is ListNode) {
                    } else if (r is ListNode) {
                    }
                }
            }

            return null;
        }
    }
}
